import cv2

img = cv2.imread(r'D:\\Image\\face1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face = cv2.CascadeClassifier('D:\\OpenCV\\build\\etc\\'
                             'haarcascades\\haarcascade_frontalface_default.xml')
eye = cv2.CascadeClassifier('D:\\OpenCV\\build\\etc\\'
                            'haarcascades\\haarcascade_eye.xml')

faces = face.detectMultiScale(gray)
for x, y, w, h in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
    roi_eye = gray[y:y + h, x:x + w]
    eyes = eye.detectMultiScale(roi_eye)
    for (ex, ey, ew, eh) in eyes:
        # 注意这里第一个参数修改为img，同时坐标位置基于(x,y)这个左上角坐标进行偏移计算
        cv2.rectangle(img,
                      (x + int(ex + ew / 2), y + int(ey + eh / 2)),
                      (x + int(max(ew, eh) / 2), y + int(ey + eh / 2)),
                      (0, 255, 0), 2)

cv2.imshow('face', img)
cv2.waitKey(0)